package com.sxp.dao;

import com.sxp.model.FoodSelection;
import com.sxp.model.User;
import com.sxp.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lakmini on 10/13/14.
 */
public class UserDaoImpl implements UserDao,FoodSelectionDao{

    @Override
    public void addUser(User user) {
        Connection conn=null;
        try{
            conn=DBUtil.getConnection();
            PreparedStatement preparedStatement=conn.prepareStatement("insert into user(username,pwd,role,name) values(?,?,?,?)");
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, String.valueOf(user.getRole()));
            preparedStatement.setString(4, user.getName());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeConnection(conn);
        }
    }

    @Override
    public void deleteUser(String userName) {
        Connection conn=null;
        try{
            conn=DBUtil.getConnection();
            PreparedStatement preparedStatement=conn.prepareStatement("delete from user where username=?");
            preparedStatement.setString(1, userName);

            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.closeConnection(conn);
        }
    }

    @Override
    public void updateUser(User user) {
        Connection conn = null;
        try {
            conn = DBUtil.getConnection();
            PreparedStatement pStmt = conn.prepareStatement("update user set username=?, pwd=?, role=?, name=?" +
                    "where username=?");

            pStmt.setString(1, user.getUserName());
            pStmt.setString(2, user.getPassword());
            pStmt.setString(3, String.valueOf(user.getRole()));
            pStmt.setString(4, user.getName());

            pStmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.closeConnection(conn);
        }

    }

    @Override
    public List<User> getAllUsers() {
        Connection conn = null;
        List<User> users = new ArrayList<User>();
        try {
            conn = DBUtil.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rSet = stmt.executeQuery("select * from user");
            while (rSet.next()) {
                User user = new User();
                user.setUserName(rSet.getString("username"));
                user.setPassword(rSet.getString("pwd"));
                user.setRole(rSet.getString("role"));
                user.setName(rSet.getString("name"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.closeConnection(conn);
        }

        return users;
    }

    @Override
    public User findUser(String userName) {
        Connection conn = null;
        User user = new User();
        try {
            conn = DBUtil.getConnection();
            PreparedStatement pStmt = conn.prepareStatement("select username,role,name from user where username=?");
            pStmt.setString(1, userName);
            ResultSet rSet = pStmt.executeQuery();

            if (rSet.next()) {
                user.setUserName(rSet.getString("username"));
                user.setRole(rSet.getString("role"));
                user.setName(rSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            DBUtil.closeConnection(conn);
        }

        return user;
    }

    @Override
    public void addFoodSelection(FoodSelection foodSelection) {

    }

    @Override
    public void deleteFoodSelection() {

    }
}
